package com.kindness.frate.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.ImageView;

import com.kindness.frate.R;
import com.kindnesslibrary.adapter.CommonAdapter;
import com.kindnesslibrary.adapter.ViewHolder;
import com.kindnesslibrary.base.BaseActivity;
import com.kindnesslibrary.base.BaseApplication;
import com.kindnesslibrary.util.AlbumHelper;
import com.kindnesslibrary.util.Contants;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 阿里
 */
public class AliPayBehaviorActivity extends BaseActivity {

    RecyclerView rv_behavior;
    private AlbumHelper helper;// 图片集帮助类
    CommonAdapter commonAdapter;
    List<String> data = new ArrayList<>();

    @Override
    protected void handlerMessage(Message msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_ali_pay_behavior_layout;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        helper = AlbumHelper.getInstance();
        rv_behavior = (RecyclerView) findViewById(R.id.rv_behavior);
        rv_behavior.setLayoutManager(new GridLayoutManager(this, 3));
    }

    @Override
    protected void initEvent() {
        // 检查权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) ==
                PackageManager.PERMISSION_GRANTED) {
            getPhotos(); // 已经获取读权限则获取读取相片资源
        } else {
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE}, Contants.ALBUM_CONTENT_DATA_CODE); // 请求读权限
        }
        commonAdapter = new CommonAdapter<String>(this, R.layout.item_ximage_layout, new ArrayList<String>()) {
            @Override
            public void convert(ViewHolder holder, String localPath) {
                ImageView ivXimageImage = holder.getView(R.id.iv_ximage_image);
                ImageLoader.getInstance().displayImage("file://" + localPath, ivXimageImage, BaseApplication.getInstance().options, null);
            }
        };
        rv_behavior.setAdapter(commonAdapter);
    }

    /**
     * 获取相册
     */
    private void getPhotos() {
        startRunnable(() -> {
            ArrayList<Map<String, Object>> albumList = helper.getImagesBucketList();
            if (albumList != null)
                for (int i = 0; i < albumList.size(); i++) {
                    if (albumList.get(i) == null) {
                        albumList.remove(i--);
                    }
                }
            for (Map<String, Object> album : albumList) {
                if (album != null && album.size() > 0) {
                    List<Map<String, String>> contentParams = ((List<Map<String, String>>) album.get("imageList"));
                    for (Map<String, String> contentMap : contentParams){
                        if(!TextUtils.isEmpty(contentMap.get("thumbnailPath")) && new File(contentMap.get("thumbnailPath")).exists()) {
                            data.add(contentMap.get("thumbnailPath"));
                        }
                    }
                }
            }
            handler.post(() -> {
                commonAdapter.addAllData(data);
            });
        });
    }

}
